home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 729 / dsound / source / playmono2.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  5KB  |  187 lines

  1.  
  2. /*************************************************************************/
  3. /*                  PlayMono2.c                 */
  4. /*     Contains code used to play mono samples out of both speakers.     */
  5. /*************************************************************************/
  6.  
  7. #include <exec/types.h>
  8. #include <exec/exec.h>
  9. #include <devices/audio.h>
  10. #include <dos/dos.h>
  11. #include <intuition/intuition.h>
  12. #include <intuition/intuitionbase.h>
  13. #include <graphics/gfxbase.h>
  14. #include <stdlib.h>
  15.  
  16. #include "dsound.h"
  17.  
  18. #include <proto/intuition.h>
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21.  
  22. extern UBYTE rightAMap[];
  23. extern UBYTE leftAMap[];
  24. extern UBYTE eitherAMap[];
  25. extern UBYTE bothAMap[];
  26.  
  27. extern UBYTE volume;
  28. extern UWORD speed;
  29. extern ULONG bufSize;
  30.  
  31. extern BOOL readAll;
  32. extern BOOL loop;
  33. extern struct Window *window;
  34.  
  35. extern ULONG signalMask;
  36.  
  37. /*Play a mono sample (or a single channel of a stereo sample) out of */
  38. /*both speakers simultaneously*/
  39. void playMonoTwice(BPTR file,channel audioChannel,struct Voice8Header *vhdr,
  40.            ULONG length)
  41. {
  42.    struct IOAudio *iob1_right,*iob2_right,*iob1_left,*iob2_left;
  43.    struct IOAudio *cur_right,*cur_left,*alt_right,*alt_left;
  44.    ULONG toRead;
  45.    ULONG sampleSize=length;
  46.    BOOL done=FALSE;
  47.  
  48.    /*Read the entire sample into memory, if the user so specified*/
  49.    if(readAll)
  50.    {
  51.       storeLeft(file,length,bufSize);
  52.       file=0L;
  53.    }
  54.  
  55.    /*Get the first audio channel*/
  56.    iob1_left=GetAudioChannel(bufSize,leftAMap);
  57.    if(iob1_left==NULL)
  58.    {
  59.       WriteMsg("Couldn't create the first stereo buffer\n");
  60.       cleanup(150);
  61.    }
  62.  
  63.    iob1_right=GetAudioChannel(bufSize,rightAMap);
  64.    if(iob1_right==NULL)
  65.    {
  66.       WriteMsg("Couldn't create the second stereo buffer\n");
  67.       cleanup(150);
  68.    }
  69.    FreeMem(iob1_right->ioa_Data,iob1_right->ioa_Length);
  70.    iob1_right->ioa_Data=iob1_left->ioa_Data;
  71.  
  72.    /* If the user didn't specify a volume, get it from the VHDR */
  73.    if(volume==0)
  74.       volume=(vhdr->volume>>10);
  75.  
  76.    /* If the VHDR gave a volume of zero, use maximum volume*/
  77.    if(volume==0)
  78.       volume=64;
  79.  
  80.    /* Get the samples/sec rate (either the rate given by the user, or the*/
  81.    /* rate found in the VHDR) */
  82.    if(speed==0)
  83.       speed=1000000000/(vhdr->samplesPerSec*279);
  84.    else
  85.       speed=1000000000/(speed*279);
  86.  
  87.    InitAudioChannel(iob1_left,volume,speed);
  88.    InitAudioChannel(iob1_right,volume,speed);
  89.  
  90.    /*Get the 2nd audio channel*/
  91.    iob2_left=DuplicateAudioChannel(iob1_left);
  92.  
  93.    if(iob2_left==NULL)
  94.    {
  95.       FreeAudioChannel(iob1_left);
  96.       FreeAudioChannel(iob1_right);
  97.       WriteMsg("Couldn't create the second buffer");
  98.       cleanup(175);
  99.    }
  100.  
  101.    iob2_right=DuplicateAudioChannel(iob1_right);
  102.    if(iob2_right==NULL)
  103.    {
  104.       FreeAudioChannel(iob1_left);
  105.       DeleteDuplication(iob2_left);
  106.       FreeAudioChannel(iob1_right);
  107.       WriteMsg("Couldn't create the second buffer");
  108.       cleanup(175);
  109.    }
  110.    FreeMem(iob2_right->ioa_Data,iob2_right->ioa_Length);
  111.    iob2_right->ioa_Data=iob2_left->ioa_Data;
  112.  
  113.    /* Load the first buffer*/
  114.    toRead=MIN(length,bufSize);
  115.    LoadAudioBuffer(file,iob1_left,toRead);
  116.    iob1_right->ioa_Length=iob1_left->ioa_Length=toRead;
  117.    length-=toRead;
  118.  
  119.    if(length==0 && loop)
  120.    {
  121.       length=sampleSize;
  122.       Seek(file,-sampleSize,OFFSET_CURRENT);
  123.    }
  124.  
  125.    /*Queue up the play requests*/
  126.    BeginIO((struct IORequest *)iob1_left);
  127.    BeginIO((struct IORequest *)iob1_right);
  128.  
  129.    cur_right=iob2_right;
  130.    cur_left=iob2_left;
  131.    alt_right=iob1_right;
  132.    alt_left=iob1_left;
  133.  
  134.    /*Loop while there's stuff to read*/
  135.    while(!done)
  136.    {
  137.       toRead=MIN(length,bufSize);
  138.  
  139.       if(toRead!=0)
  140.       {
  141.      LoadAudioBuffer(file,cur_left,toRead);
  142.      cur_right->ioa_Length=cur_left->ioa_Length=toRead;
  143.      length-=toRead;
  144.      BeginIO((struct IORequest *)cur_left);
  145.      BeginIO((struct IORequest *)cur_right);
  146.  
  147.      if(length==0 && loop)
  148.      {
  149.         length=sampleSize;
  150.         Seek(file,-sampleSize,OFFSET_CURRENT);
  151.      }
  152.      done=FALSE;
  153.       }
  154.       else
  155.      done=TRUE;
  156.  
  157.       /*Wait for the second buffer to finish*/
  158.       if((Wait((1<<alt_right->ioa_Request.io_Message.mn_ReplyPort->mp_SigBit) |
  159.        signalMask) & SIGBREAKF_CTRL_C) == SIGBREAKF_CTRL_C)
  160.      done=TRUE;
  161.  
  162.       if(window!=NULL && GetMsg(window->UserPort)!=NULL)
  163.       {
  164.      done=TRUE;
  165.       }
  166.       swapPointers(&cur_left,&alt_left);
  167.       swapPointers(&cur_right,&alt_right);
  168.    }
  169.  
  170.    /*Restore the buffer lengths, so that FreeAudio() channel, etc., knows*/
  171.    /*how much memory to free*/
  172.    iob1_left->ioa_Length=iob2_left->ioa_Length=bufSize;
  173.    iob1_right->ioa_Length=iob2_right->ioa_Length=bufSize;
  174.  
  175.    iob1_right->ioa_Data=NULL;
  176.    iob2_right->ioa_Data=NULL;
  177.  
  178.    FreeAudioChannel(iob1_left);
  179.    DeleteDuplication(iob2_left);
  180.    FreeAudioChannel(iob1_right);
  181.    DeleteDuplication(iob2_right);
  182.  
  183.    return;
  184. }
  185.  
  186. /*End of Play.c*/
  187.